<?php

namespace app\models;

use Yii;
use yii\helpers\Html;
use app\models\ProjectCategoryRelation;
/**
 * This is the model class for table "{{%project}}".
 *
 * @property int $id
 * @property string $name 应用名称
 * @property string $email 联系邮箱
 * @property string $own 项目所有者
 * @property string $brief_desc 项目简介
 * @property int $category 项目类别
 * @property string $web_link 项目web链接
 * @property string $app_link 项目app链接
 * @property string $certify 网络许可证明
 * @property string $desc 项目完整描述
 * @property string $logo_url 项目logo url
 * @property int $progress 项目完成进度
 * @property string $contract_addr 合约地址json数据
 * @property string $social_link 社交媒体链接
 * @property string $label 项目标签
 * @property int $created_time 项目创建时间
 * @property int $updated_time 项目更新时间
 */
class Project extends \yii\db\ActiveRecord
{   
    //审核状态
    const CHECK_STATUS_WATING = 0;//待审核
    const CHECK_STATUS_NOT_PASS = 1;//不通过
    const CHECK_STATUS_PASS = 2;//通过

    //项目进度
    const PROGRESS_STATUS_0 = 0;//测试
    const PROGRESS_STATUS_1 = 1;//发布
    
    public static function getProgressStatus($status=null, $field=null){
         $arr = [
            self::PROGRESS_STATUS_0 => ['id' =>self::PROGRESS_STATUS_0, 'name' => '测试', 'class' =>'label-warning btn-xs'],
            self::PROGRESS_STATUS_1 => ['id' =>self::PROGRESS_STATUS_1,'name' => '发布', 'class' =>'label-success btn-xs'],
        ];
        if (isset($arr[$status][$field])) {
            return $arr[$status][$field];
        }elseif(isset($arr[$status])){
            return Html::tag('span', $arr[$status]['name'], ['class' => 'lable '.$arr[$status]['class']]);
        }

        return array_column($arr, 'name', 'id');
    }

    public static function getCheckStatus($status=null, $field=null){
        $arr = [
            self::CHECK_STATUS_WATING => ['name' => '审核中', 'class' =>'label-warning btn-xs'],
            self::CHECK_STATUS_NOT_PASS => ['name' => '不通过', 'class' =>'label-danger btn-xs'],
            self::CHECK_STATUS_PASS => ['name' => '通过', 'class' =>'label-success btn-xs'],
        ];
        if (isset($arr[$status][$field])) {
            return $arr[$status][$field];
        }elseif(isset($arr[$status])){
            return Html::tag('span', $arr[$status]['name'], ['class' => 'lable '.$arr[$status]['class']]);
        }

        return $arr;
    }

    //获取
    public static function getCheckStatusKeyPair($field='name'){
        $data = self::getCheckStatus();

        $retuen_arr = [];
        foreach ($data as $key => $value) {
            $retuen_arr[$key] = $value[$field];
        }
        return $retuen_arr;
    }

    //获取所有分类
    // public static function getProjectCategory(){
    //     $model = new ProjectCategory;
    //     $cate = $model->getAllCategory();
    //     return $model->getDropDownList($cate);
    // }

    public function attributes()
    {   
        // 添加关联字段到可搜索属性集合
        return array_merge(parent::attributes(), ['username']);
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%project}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            // [['name'], 'required', 'message' => '名字不能为空'],
            // [['email'], 'required', 'message' => '邮箱不能为空'],
            // [['email'], 'email', 'message' =>'邮箱格式不对'],
            // [['category', 'progress', 'created_time', 'updated_time'], 'integer'],
            // [['desc', 'contract_addr', 'social_link'], 'string'],
            // [['name', 'own', 'brief_desc', 'web_link', 'app_link', 'certify', 'logo_url'], 'string', 'max' => 255],
            
            // [['label'], 'string', 'max' => 50],
            [['status'], 'integer'],
            [['opt_note', 'name', 'email'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'name' => Yii::t('app', '项目名称'),
            'email' => Yii::t('app', '联系邮箱'),
            'own' => Yii::t('app', '项目所有者'),
            'uid' => Yii::t('app', '项目上传者'),
            'brief_desc' => Yii::t('app', '项目简介'),
            'category' => Yii::t('app', '项目类别'),
            'web_link' => Yii::t('app', '项目web链接'),
            'app_link' => Yii::t('app', '项目app链接'),
            'certify' => Yii::t('app', '网络许可证明'),
            'desc' => Yii::t('app', '项目完整描述'),
            'logo_url' => Yii::t('app', '项目logo url'),
            'progress' => Yii::t('app', '项目进度'),
            'contract_addr' => Yii::t('app', '合约地址json数据'),
            'social_link' => Yii::t('app', '社交媒体链接'),
            'label' => Yii::t('app', '项目标签'),
            'created_time' => Yii::t('app', '申请时间'),
            'updated_time' => Yii::t('app', '项目更新时间'),
            'status' => Yii::t('app', '审核状态'),
            'opt_note' => Yii::t('app', '备注'),
            'big_cate' =>Yii::t('app', '类别'),
        ];
    }

    //使用图片轮播插件
    public function getPictrues($json_picture){
        return '<img src="'.$json_picture.'" width="80px" height="80px">';
    }

    public function getCategoryRelations()
    {
        return $this->hasMany(ProjectCategoryRelation::className(), ['project_id' => 'id']);
    }
}
